package src.数据结构.栈;

public class 括号匹配 {

    public static boolean isMatch(String str){
        Stack<Character> stack = new Stack<>();
        for (char c : str.toCharArray()) {
            if(c=='('){
                stack.push(c);
            }
            else if (c==')') {
                Character pop = stack.pop();
                if (pop == null) {
                    System.out.println("右括号多了");
                    return false;
                }
            }
        }
        if(stack.size()==0){
            System.out.println("括号对应");
            return true;
        }
        System.out.println("左括号多了");
        return false;
    }

    public static void main(String[] args) {
        // 判断字符串内的小括号是否匹配

        String str = "(长安),(上海)";

        System.out.println(isMatch(str));
    }
}
